#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""查看筛选结果"""

import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

import pandas as pd
from glob import glob
import os

# 查找最新的Excel文件
files = glob("A股筛选_Baostock_*.xlsx")
if files:
    latest_file = max(files, key=os.path.getctime)
    print(f"读取文件: {latest_file}\n")
    
    # 读取筛选结果sheet
    df = pd.read_excel(latest_file, sheet_name='筛选结果')
    
    print("="*100)
    print(f"{'A股智能筛选结果':^100}")
    print("="*100)
    print(f"\n共筛选出 {len(df)} 支股票:\n")
    
    # 显示主要列
    display_cols = ['代码', '名称', '最新价', '总市值(亿)', '3个月涨跌幅', '52周回撤', '平均换手率', '综合得分', '推荐理由']
    
    for idx, row in df.iterrows():
        print(f"{idx+1}. {row['代码']} - {row['名称']}")
        print(f"   价格: {row['最新价']:.2f}元  |  市值: {row['总市值(亿)']:.1f}亿")
        print(f"   3月涨跌: {row['3个月涨跌幅']:.1f}%  |  回撤: {row['52周回撤']:.1f}%  |  换手: {row['平均换手率']:.2f}%")
        print(f"   得分: {row['综合得分']:.1f}  |  {row['推荐理由']}")
        print()
    
    # 读取统计信息
    stats_df = pd.read_excel(latest_file, sheet_name='统计信息')
    print("="*100)
    print(f"{'筛选统计':^100}")
    print("="*100)
    for _, row in stats_df.iterrows():
        print(f"  {row['项目']}: {row['数量']} 支")
    print("="*100)
else:
    print("[WARN] 未找到筛选结果文件")
